﻿
using System.IO.Ports;
using System;
using MainCableTest.ViewModel;


namespace MainCableTest.Channel
{
    public class KeyBoardSerialPort
    {
        private SerialPort keyBoardSerialPort;

        private SerialPort receiveDataSerialPort = new SerialPort();

        public KeyBoardSerialPort()
        {
            
        }

        public void KeepListening()
        {
            string[] portNames = { "COM3" };
            int baudRate = 115200;
            SystemModel model = SystemModel.GetGlobalInstance();
            for (int index = 0; index < portNames.Length; index++)
            {
                try
                {
                    keyBoardSerialPort = new SerialPort(portNames[index], baudRate, Parity.None, 8, StopBits.One);
                    //keyBoardSerialPort.ReadTimeout = 1000;
                    //keyBoardSerialPort.DataReceived += DataReceivedHandler;
                    keyBoardSerialPort.Open();
                }
                catch (Exception e)
                {
                    //DateTime currentTime = DateTime.Now;
                    //string time = currentTime.ToString("yyyy/MM/dd HH:mm:ss");
                    //string temp = time + " " + model.PrintFileInfo();
                    //temp += "未连接到操控手柄";
                    //model.ReceiveRobotMessage(temp, MessageState.Error);
                    //keyBoardSerialPort.Close();
                }
                finally
                {
                    if (keyBoardSerialPort.IsOpen)
                    {
                        //DateTime currentTime = DateTime.Now;
                        //string time = currentTime.ToString("yyyy/MM/dd HH:mm:ss");
                        //string temp = time + " " + model.PrintFileInfo();
                        //temp += "连接到操控手柄";
                        //model.ReceiveRobotMessage(temp, MessageState.Normal);
                    }
                }
            }
        }
    }
}
